ALTER TABLE PQMeasurement
ADD Unit VARCHAR(200) NOT NULL CONSTRAINT DF_PQMeasurement_Unit DEFAULT('')
GO

ALTER TABLE PQMeasurement
DROP CONSTRAINT DF_PQMeasurement_Unit
GO

UPDATE PQMeasurement
SET Unit = (SELECT Name FROM Unit WHERE ID = PQMeasurement.UnitID)
GO

DECLARE @FK_PQMeasurement_Unit NVARCHAR(100)
SELECT @FK_PQMeasurement_Unit = CONCAT('ALTER TABLE PQMeasurement DROP CONSTRAINT ', (SELECT name 
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('Unit') AND parent_object_id = object_id('PQMeasurement')
))
EXECUTE sp_executesql @FK_PQMeasurement_Unit
GO
ALTER TABLE PQMeasurement
DROP COLUMN UnitID
GO

DECLARE @FK_PQMeasurement_Phase NVARCHAR(100)
SELECT @FK_PQMeasurement_Phase = CONCAT('ALTER TABLE PQMeasurement DROP CONSTRAINT ', (SELECT name 
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('Phase') AND parent_object_id = object_id('PQMeasurement')
))
EXECUTE sp_executesql @FK_PQMeasurement_Phase
GO
ALTER TABLE PQMeasurement
DROP COLUMN PhaseID
GO

DECLARE @DF_PQMeasurement_HarmonicGroup NVARCHAR(100)
SELECT @DF_PQMeasurement_HarmonicGroup = CONCAT('ALTER TABLE PQMeasurement DROP CONSTRAINT ', (SELECT dc.name 
FROM sys.default_constraints dc JOIN sys.columns c ON c.default_object_id = dc.object_id
WHERE c.name = 'HarmonicGroup' AND dc.parent_object_id = object_id('PQMeasurement')
))
EXECUTE sp_executesql @DF_PQMeasurement_HarmonicGroup
GO
ALTER TABLE PQMeasurement
DROP COLUMN HarmonicGroup
GO

DECLARE @DF_PQMeasurement_Enabled NVARCHAR(100)
SELECT @DF_PQMeasurement_Enabled = CONCAT('ALTER TABLE PQMeasurement DROP CONSTRAINT ', (SELECT dc.name 
FROM sys.default_constraints dc JOIN sys.columns c ON c.default_object_id = dc.object_id
WHERE c.name = 'Enabled' AND dc.parent_object_id = object_id('PQMeasurement')
))
EXECUTE sp_executesql @DF_PQMeasurement_Enabled
GO
ALTER TABLE PQMeasurement
DROP COLUMN Enabled
GO

CREATE NONCLUSTERED INDEX IX_PQMeasurement_MeasurementTypeID
ON PQMeasurement(MeasurementTypeID ASC)
GO

CREATE NONCLUSTERED INDEX IX_PQMeasurement_MeasurementCharacteristicID
ON PQMeasurement(MeasurementCharacteristicID ASC)
GO

DROP TABLE Unit
GO

DROP TABLE PQTrendStat
GO

DROP TABLE StepChangeStat
GO

DROP TABLE StepChangeMeasurement
GO

ALTER TABLE StandardMagDurCurve
DROP COLUMN XHigh
GO

ALTER TABLE StandardMagDurCurve
DROP COLUMN XLow
GO

ALTER TABLE StandardMagDurCurve
DROP COLUMN YHigh
GO

ALTER TABLE StandardMagDurCurve
DROP COLUMN YLow
GO

ALTER TABLE StandardMagDurCurve
DROP COLUMN UpperCurve
GO

ALTER TABLE StandardMagDurCurve
DROP COLUMN LowerCurve
GO

ALTER TABLE StandardMagDurCurve
ADD Color varchar(255) NOT NULL CONSTRAINT DF_StandardMagDurCurve_Color DEFAULT('#000000')
GO

ALTER TABLE StandardMagDurCurve
DROP CONSTRAINT DF_StandardMagDurCurve_Color
GO

UPDATE StandardMagDurCurve SET Color = '#007a29' WHERE Name = 'ITIC'
GO
UPDATE StandardMagDurCurve SET Color = '#edc240' WHERE Name = 'SEMI F47'
GO
UPDATE StandardMagDurCurve SET Color = '#a30000' WHERE Name = 'IEEE 1668 Type I & II'
GO
UPDATE StandardMagDurCurve SET Color = '#185aa9' WHERE Name = 'IEEE 1668 Type III'
GO
UPDATE StandardMagDurCurve SET Color = '#d3d3d3' WHERE Name = 'NERC PRC-024-2'
GO

INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Transients', NULL, '#afd8f8')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Instantaneous Sag', NULL, '#f47d23')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Instantaneous Swell', NULL, '#008c48')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Mom. Interruption', NULL, '#ee2e2f')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Momentary Sag', NULL, '#737373')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Momentary Swell', NULL, '#662c91')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Temp. Interruption', NULL, '#bd9b33')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Temporary Sag', NULL,'#ff904f')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Temporary Swell', NULL, '#ff9999')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Sustained Int.', NULL, '#0029A3')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Undervoltage', NULL, '#cb4b4b')
GO
INSERT StandardMagDurCurve (Name, Area, Color) VALUES (N'IEEE 1159 Overvoltage', NULL, '#4da74d')
GO

UPDATE StandardMagDurCurve SET Area = 'POLYGON((0.000001 0, 0.01 0, 0.01 5.0, 0.000001 5.0, 0.000001 0))' WHERE Name = 'IEEE 1159 Transients'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((0.01 0.1, 0.5 0.1, 0.5 0.9, 0.01 0.9, 0.01 0.1))' WHERE Name = 'IEEE 1159 Instantaneous Sag'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((0.01 1.1, 0.5 1.1, 0.5 1.8, 0.01 1.8, 0.01 1.1))' WHERE Name = 'IEEE 1159 Instantaneous Swell'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((0.01 0, 3.0 0, 3.0 0.1, 0.01 0.1, 0.01 0))' WHERE Name = 'IEEE 1159 Mom. Interruption'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((0.5 0.1, 3.0 0.1, 3 0.9, 0.5 0.9, 0.5 0.1))' WHERE Name = 'IEEE 1159 Momentary Sag'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((0.5 1.1, 3.0 1.1, 3.0 1.4, 0.5 1.4, 0.5 1.1))' WHERE Name = 'IEEE 1159 Momentary Swell'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((3.0 0, 60.0 0, 60.0 0.1, 3.0 0.1, 3.0 0))' WHERE Name = 'IEEE 1159 Temp. Interruption'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((3.0 0.1, 60.0 0.1, 60.0 0.9, 3.0 0.9, 3.0 0.1))' WHERE Name = 'IEEE 1159 Temporary Sag'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((3.0 1.1, 60.0 1.1, 60.0 1.2, 3.0 1.2, 3.0 1.1))' WHERE Name = 'IEEE 1159 Temporary Swell'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((60.0 0, 1000.0 0, 1000.0 0.1, 60.0 0.1, 60.0 0))' WHERE Name = 'IEEE 1159 Sustained Int.'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((60.0 0.8, 1000.0 0.8, 1000.0 0.9, 60.0 0.9, 60.0 0.8))' WHERE Name = 'IEEE 1159 Undervoltage'
GO
UPDATE StandardMagDurCurve SET Area = 'POLYGON((60.0 1.1, 1000.0 1.1, 1000.0 1.2, 60.0 1.2, 60.0 1.1))' WHERE Name = 'IEEE 1159 Overvoltage'
GO

INSERT INTO TriggeredEmailDatasource (Name, AssemblyName, TypeName,ConfigUI) VALUES
('FTT','openXDA.NotificationDataSources.dll','openXDA.NotificationDataSources.FaultTraceTool.FTTDataSource','ftt')
GO